package com.right.util;

import io.netty.util.internal.StringUtil;

import java.io.UnsupportedEncodingException;

/**********************************************
 * @author pillar
 * @date 2018/5/28
 **********************************************/
public final class ByteUtil {

    /**
     * int转2位长度的byte[]
     * @param len
     * @return
     */
    public static byte[] intToByte(int len) {
        byte[] targets = new byte[2];
        targets[0] = (byte) (len & 0xFF);
        targets[1] = (byte) (len >> 8 & 0xFF);
        return targets;
    }

    /**
     * int转2位长度的byte[]
     * @param len
     * @return
     */
    public static byte[] intToByteCompose(int len) {
        byte[] targets = new byte[2];
        targets[1] = (byte) (len & 0xFF);
        targets[0] = (byte) (len >> 8 & 0xFF);
        return targets;
    }
    /**
     * 2位byte[]求长度
     * @param len
     * @return
     */
    public static int byteToInt(byte[] len){
        return len[0]<<8|len[1] & 0xFF;
    }

    /**
     * 2位byte[]求长度
     * @param pos
     * @return
     */
    public static int byteToInt(byte[] data,int pos){
        return data[pos]<<8|data[pos + 1] & 0xFF;
    }


    /**
     * 2位byte[]求长度
     * @param pos
     * @return
     */
    public static int byteToIntConvert(byte[] data,int pos){
        return data[pos + 1]<<8|data[pos] & 0xFF;
    }
    /**
     * byte 转int，无符号
     * @param b
     * @return
     */
    public static int byteToInt(byte b){
        return b & 0xFF;
    }


    /**
     * int转十六进制
     * @param i
     * @return
     */
    public static String byteToHex(int i){
        return StringUtil.byteToHexString(i);
    }

    /**
     * 获取string类型数据
     * @param bytes
     * @param pos
     * @param len
     * @return
     */
    public static String getString(byte[] bytes,int pos,int len){
        try {
            return new String(bytes,pos,len,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    /**
     * 获取string类型数据
     * @param bytes
     * @return
     */
    public static String getString(byte[] bytes){
        return getString(bytes,0,bytes.length);
    }

    /**
     * byte取前4位
     * @param b
     * @return
     */
    public static int getLeft(byte b){
        return b>>4 & 15;
    }

    /**
     * byte取后4位
     * @param b
     * @return
     */
    public static int getRight(byte b){
        return b & 15;
    }

    /**
     * byte截取
     * @param b
     * @param pos
     * @param len
     * @return
     */
    public static String subString(int b,int pos,int len){
        String s = Integer.toBinaryString(b);
        String str = String.format("%08d", Integer.valueOf(s));
        return str.substring(pos,len + pos);
    }

    /**
     * 获取01字符串
     * @param b
     * @return
     */
    public static String getBinaryString(int b){
        String s = Integer.toBinaryString(b);
        return String.format("%08d", Integer.valueOf(s));
    }
    /**
     * 叫数据的位数，前四位掉换成后四bit位
     * @param a
     * @return
     */
    public static byte swap(byte a) {
        int lowBit = a & 0xF;
        int highBit = a & 0xF0;
        return (byte)(lowBit << 4 | highBit >>> 4);
    }
    /**
     * 将int数值转换为占四个字节的byte数组，本方法适用于(低位在前，高位在后)的顺序。
     * @param value
     *            要转换的int值
     * @return byte数组
     */
    public static byte[] intToBytes(int value) {
        byte[] byteSrc = new byte[4];
        byteSrc[3] = (byte) ((value & 0xFF000000)>>24);
        byteSrc[2] = (byte) ((value & 0x00FF0000)>>16);
        byteSrc[1] = (byte) ((value & 0x0000FF00)>>8);
        byteSrc[0] = (byte) ((value & 0x000000FF));
        return byteSrc;
    }

    /**
     * byte数组中取int数值，本方法适用于(低位在前，高位在后)的顺序。
     *
     * @param ary
     *            byte数组
     * @param offset
     *            从数组的第offset位开始
     * @return int数值
     */
    public static int bytesToInt(byte[] ary, int offset) {
        int value;
        value = ((ary[offset]&0xFF)
                | ((ary[offset+1]<<8) & 0xFF00)
                | ((ary[offset+2]<<16)& 0xFF0000)
                | ((ary[offset+3]<<24) & 0xFF000000));
        return value;
    }

    public static int getIntFromBytes(byte high_h, byte high_l, byte low_h, byte low_l) {
        return (high_h & 0xff) << 24 | (high_l & 0xff) << 16 | (low_h & 0xff) << 8 | low_l & 0xff;
    }

    /**
     * dword转int
     * @param data
     * @param offset
     * @return
     */
    public static int getIntFromDword(byte[] data,int offset) {
        return (data[offset] & 0xff) << 24 | (data[offset + 1] & 0xff) << 16 | (data[offset + 2] & 0xff) << 8 | data[offset + 3] & 0xff;
    }
}
